package com.zl.cms.dao;

import com.zl.basic.dao.BaseDao;
import com.zl.basic.model.Pager;
import com.zl.cms.model.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * User: zl email:c3tc3tc3t@126.com Date: 14-1-14 Time: 下午12:32
 */
@SuppressWarnings("unchecked")
@Repository("userDao")
public class UserDao extends BaseDao<User> implements IUserDao {


  @Override
  public List<Role> listUserRoles(int userid) {
    String hql = "select ur.role from UserRole ur where ur.user.id = ? ";
    return this.getSession().createQuery(hql).setParameter(0, userid).list();
  }

  @Override
  public List<Integer> listUserRoleIds(int userid) {
    String hql = "select ur.role.id from UserRole ur where ur.user.id = ? ";
    return this.getSession().createQuery(hql).setParameter(0, userid).list();
  }

  @Override
  public List<Group> listUserGroups(int userid) {
    String hql = "select  ug.group from UserGroup ug where ug.user.id = ? ";
    return this.getSession().createQuery(hql).setParameter(0, userid).list();
  }

  @Override
  public List<Integer> listUserGroupsIds(int userid) {
    String hql = "select ug.group.id from UserGroup ug where ug.user.id = ? ";
    return this.getSession().createQuery(hql).setParameter(0, userid).list();
  }

  @Override
  public UserRole loadUserRole(int userid, int roleid) {
    // 采用 join fetch 不使用延迟加载。可以减少2条sql语句发出
    // 原语句from UserRole where user.id = ? and role.id = ? 会发三条
    String hql =
        "select  ur from UserRole ur left join fetch ur.user u left join fetch ur.role r where u.id = ? and r.id = ? ";
    return (UserRole) this.getSession().createQuery(hql).setParameter(0, userid)
        .setParameter(1, roleid).uniqueResult();
  }

  @Override
  public UserGroup loadUserGroup(int userid, int groupid) {
    // 采用 join fetch 不使用延迟加载。可以减少2条sql语句发出
    // 原语句from UserGroup where user.id = ? and group.id = ?" 会发三条
    String hql =
        "select ug from UserGroup ug left join fetch ug.user u left join fetch ug.group g  where u.id = ? and g.id = ? ";
    return (UserGroup) this.getSession().createQuery(hql).setParameter(0, userid)
        .setParameter(1, groupid).uniqueResult();
  }

  @Override
  public User loadByUsername(String username) {
    String hql = "from User where username = ?";
    return (User) this.queryObjectByHql(hql, username);
  }

  @Override
  public List<User> listUsersByRoleId(int roleid) {
    String hql = "select ur.user from UserRole ur where ur.role.id=?";
    return this.list(hql, roleid);
  }

  @Override
  public List<User> listUsersByRoleType(RoleType roletype) {
    String hql = "select ur.user from UserRole ur where ur.role.roleType=?";
    return this.list(hql, roletype);
  }

  @Override
  public List<User> listUsersByGroupId(int groupid) {
    String hql = "select ug.user from UserGroup ug where ug.group.id=?";
    return this.list(hql, groupid);
  }

  @Override
  public void addUserRole(User user, Role role) {
    UserRole ur = this.loadUserRole(user.getId(), role.getId());
    if (null != ur) return;
    ur = new UserRole();
    ur.setUser(user);
    ur.setRole(role);
    this.getSession().save(ur);

  }

  @Override
  public void addUserGroup(User user, Group group) {
    UserGroup ug = this.loadUserGroup(user.getId(), group.getId());
    if (null != ug) return;
    ug = new UserGroup();
    ug.setUser(user);
    ug.setGroup(group);
    this.getSession().save(ug);
  }



  @Override
  public void deleteUserRole(int uid) {
    this.checkObjectNull(uid);
    // 删除
    String hql = "delete  UserRole ur where ur.user.id = ? ";
    this.updateByHql(hql, uid);
  }

  @Override
  public void deleteUserGroup(int uid) {
    this.checkObjectNull(uid);
    // 删除
    String hql = "delete UserGroup ug where ug.user.id = ? ";
    this.updateByHql(hql, uid);
  }

  @Override
  public Pager<User> findUser() {

    return this.find("from User");
  }

  /**
   * 检查用户是否存在
   * 
   * @param id 用户id
   */
  @Override
  public void checkObjectNull(int id) {
    super.checkObjectNull(id);
  }

  @Override
  public void deleteUserRole(int uid, int rid) {
    String hql = "delete UserRole ur where ur.user.id = ? and ur.role.id = ?";
    this.updateByHql(hql, new Object[] {uid, rid});
  }

  @Override
  public void deleteUserGroup(int uid, int gid) {
    String hql = "delete UserGroup where user.id = ? and group.id = ?";
    this.updateByHql(hql, new Object[] {uid, gid});
  }
}
